﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Navigation;
using CUWo.Common;
using CUWo.Common.Entities;
using CUWo.Common.StorageHelper;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace CUWo.App
{
    public partial class WoPage : PhoneApplicationPage
    {
        private int IsConfirmExist = -1;
        bool IsLoadedActiveInfo = false;
        public WoPage()
        {
            InitializeComponent();
            //
            InitRom();
            //
            this.cuTrackHome.OnViewTrackPackage += cuTrackHome_OnViewTrackPackage;
        }


        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            base.OnBackKeyPress(e);
            //
            if (this.cuActiveInfo.navigatePopup != null)
            {
                this.cuActiveInfo.navigatePopup.CloseWebViewPage();
                this.cuActiveInfo.navigatePopup = null;
                e.Cancel = true;
                return;
            }
            if (this.pivotMainTrack == this.pivotMain.SelectedItem)
            {
                if (this.cuTrackHome.Visibility == Visibility.Collapsed && this.cuTrackPackage.Visibility == Visibility.Visible)
                {
                    this.cuTrackHome.Visibility = Visibility.Visible;
                    this.cuTrackPackage.Visibility = Visibility.Collapsed;
                    e.Cancel = true;
                    return;
                }
            }

            IsConfirmExist++;
            Action show = delegate()
            {
                //3秒内未再次按backkeypress则表示不再退出
                IsConfirmExist--;
            };
            if (IsConfirmExist <= 0)
            {
                MessageToastManager.Show("再按一次返回键退出应用！", MessageToastLevel.Warn, show, 60);
                e.Cancel = true;
            }


        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
        }

        private void pivotMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.pivotMainUserCenter == this.pivotMain.SelectedItem)
            {
                if (!IsInitedUserCenterTileStoryboard)
                {
                    IsInitedUserCenterTileStoryboard = true;
                    this.UserCenterTileStoryboard();
                }
                //跳转至我的栏目
                //App.Navigate("/Views/PageUserCenter.xaml");
            }
            else if (this.pivotMainActive == this.pivotMain.SelectedItem)
            {
                if (!IsLoadedActiveInfo && (!(this.cuActiveInfo.panoramaActive.Items != null && this.cuActiveInfo.panoramaActive.Items.Count > 0)))
                {
                    this.cuActiveInfo.LoadData();
                    IsLoadedActiveInfo = true;
                }
            }
            else if (this.pivotMainContent == this.pivotMain.SelectedItem)
            {
                if (!App.IsLoadedNewsHome || this.cuNewsChannel.llsNewsChannel.ItemsSource == null)
                {
                    App.IsLoadedNewsHome = true;
                    this.cuNewsChannel.BindData();
                }
            }
            else if (this.pivotMainTrack == this.pivotMain.SelectedItem)
            {
                if (this.cuTrackHome.Visibility == Visibility.Visible)
                {
                    this.cuTrackHome.BindWelcomeNotice();
                    this.cuTrackHome.wocuTrackHomeBarChart.BindTrackCurrent();
                }
            }
        }

        #region 我的栏目
        bool IsInitedUserCenterTileStoryboard = false;
        /// <summary>
        /// 我的栏目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imgUserCenter_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {

            Image img = sender as Image;
            if (img == null)
                return;
            if (string.IsNullOrWhiteSpace(img.Name))
                return;

            string pageUrl = "/Views/PageUserCenter.xaml", pageUrlParam = "?pivotitem=";
            switch (img.Name)
            {
                case "imgUserCenterUpdatePwd": App.Navigate(pageUrl, string.Concat(pageUrlParam, "pivotUpdatePassword")); break;
                case "imgUserCenterMessage": App.Navigate(pageUrl, string.Concat(pageUrlParam, "pivotMessage")); break;
                case "imgUserCenterSetting": App.Navigate(pageUrl, string.Concat(pageUrlParam, "pivotSetting")); break;
                case "imgUserCenterEmail": App.Navigate(pageUrl, string.Concat(pageUrlParam, "pivotEmail")); break;
                case "imgUserCenterMain": App.Navigate(pageUrl, string.Concat(pageUrlParam, "pivotLogin")); break;
            }
        }

        #region 动画
        /// <summary>
        /// 我的设置图片动画
        /// </summary>
        void UserCenterTileStoryboard()
        {
            StoryboardInUserCenterSetting.Begin();
            StoryboardInUserCenterSetting.Completed += (o, args) => { StoryboardInUserCenterSetting.Stop(); StoryboardOutUserCenterSetting.Begin(); };
            StoryboardOutUserCenterSetting.Completed += (o, args) => { StoryboardOutUserCenterSetting.Stop(); StoryboardInUserCenterSetting.Begin(); };
            //
            this.StoryboardInUserCenterEmail.Begin();
            StoryboardInUserCenterEmail.Completed += (o, args) => { StoryboardInUserCenterEmail.Stop(); StoryboardOutUserCenterEmail.Begin(); };
            this.StoryboardOutUserCenterEmail.Completed += (o, args) => { StoryboardOutUserCenterEmail.Stop(); StoryboardInUserCenterEmail.Begin(); };
            //
            this.StoryboardInUserCenterMessage.Begin();
            StoryboardInUserCenterMessage.Completed += (o, args) => { StoryboardInUserCenterMessage.Stop(); StoryboardOutUserCenterMessage.Begin(); };
            this.StoryboardOutUserCenterMessage.Completed += (o, args) => { StoryboardOutUserCenterMessage.Stop(); StoryboardInUserCenterMessage.Begin(); };
            //
            this.StoryboardInUserCenterMain.Begin();
            StoryboardInUserCenterMain.Completed += (o, args) => { StoryboardInUserCenterMain.Stop(); StoryboardOutUserCenterMain.Begin(); };
            this.StoryboardOutUserCenterMain.Completed += (o, args) => { StoryboardOutUserCenterMain.Stop(); StoryboardInUserCenterMain.Begin(); };
            //
            this.StoryboardInUserCenterUpdatePwd.Begin();
            StoryboardInUserCenterUpdatePwd.Completed += (o, args) => { StoryboardInUserCenterUpdatePwd.Stop(); StoryboardOutUserCenterUpdatePwd.Begin(); };
            this.StoryboardOutUserCenterUpdatePwd.Completed += (o, args) => { StoryboardOutUserCenterUpdatePwd.Stop(); StoryboardInUserCenterUpdatePwd.Begin(); };
        }
        #endregion 动画
        #endregion 我的栏目

        #region 内容页

        #endregion 内容页

        #region 流量页
        /// <summary>
        /// 查看套餐详情
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void cuTrackHome_OnViewTrackPackage(object sender, EventArgs e)
        {
            this.cuTrackHome.Visibility = Visibility.Collapsed;
            this.cuTrackPackage.Visibility = Visibility.Visible;
            //
            this.cuTrackPackage.wocuTrackPackageBarChart.BindTrackCurrent();
        }
        #endregion 流量页


        #region APP ROM
        void InitRom()
        {
            //if (!IsolatedStorageHelper.Instance.FileExists(Keys.ROM_FILENAME))
            {
                LogHelper.LogInfo("初始化App rom信息！");
                RomInfo romInfo = new RomInfo()
                {
                    //Mobile = string.Empty,
                    //Password = string.Empty,
                    Mobile = "15668469578",
                    Password = "n0yef2",
                    IsPush = true
                };
                IsolatedStorageHelper.Instance.WriteText(JSONHelper.Serialize<RomInfo>(romInfo), Keys.ROM_FILENAME, true);
            }
        }
        #endregion  APP ROM
    }
}